package com.tianruan.dc.modules.userinfo.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.tianruan.dc.common.encoding.Md5PasswordEncoder;
import com.tianruan.dc.common.utils.encryption.Md5Utils;
import com.tianruan.dc.modules.sys.user.entity.dto.RegisterDTO;
import com.tianruan.dc.modules.sys.user.entity.dto.UserDTO;
import com.tianruan.dc.modules.sys.user.utils.PageBean;
import com.tianruan.dc.modules.userinfo.context.BaseContext;
import com.tianruan.dc.modules.userinfo.entity.Admin;
import com.tianruan.dc.modules.userinfo.entity.AdminPageDTO;
import com.tianruan.dc.modules.userinfo.entity.User;
import com.tianruan.dc.modules.userinfo.mapper.AdminMapper;
import com.tianruan.dc.modules.userinfo.service.AdminService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;


@Service
@Slf4j
@Transactional
public class AdminServiceImpl implements AdminService{
    @Autowired
    private AdminMapper adminMapper;

    public AdminPageDTO getById(String id) {
        AdminPageDTO adminPageDTO=adminMapper.getById(id);
        return adminPageDTO;
    }

    public void update(Admin admin) {
        admin.setUpdateBy(BaseContext.getCurrentId());
        admin.setUpdateDate(LocalDateTime.now());
        adminMapper.update(admin);
    }


    public void delete(Admin id){
        adminMapper.setDelFlag(id);
    }


    public PageBean<AdminPageDTO> pageAdmin(Integer page, Integer pageSize, String name, String userName, String phone, String position,
                                            String deptId,String roleId,String status) {
        //创建pageBean对象
        PageBean<AdminPageDTO> pageBean = new PageBean<>();
        //开启分页查询  和 模糊查询
        PageHelper.startPage(page,pageSize);
        //调用mapper
        List<AdminPageDTO> pageInfo = adminMapper.pageQuery(name,userName,phone,position,deptId,roleId,status);
        //获取分页查询后  得到总记录数和当前页的数据
        com.github.pagehelper.Page<AdminPageDTO> page1 = (Page<AdminPageDTO>) pageInfo;
        //把数据填充到pagebean对象中
        pageBean.setTotal(page1.getTotal());
        pageBean.setItems(page1.getResult());
        //返回pagebean对象
        return pageBean;
    }


    public boolean check(Admin admin) {
        String Del=adminMapper.check(admin.getId());
        if(Objects.equals(Del, "0")){
            return true;
        }
        else {
            return false;
        }
    }


    public void updatePassword(String id) {
        User user=adminMapper.find(id);
        Md5PasswordEncoder md5PasswordEncoder = new Md5PasswordEncoder();
        user.setPassword(md5PasswordEncoder.encode("123456"));
        adminMapper.updatePassword(user.getPassword(),id);
    }


    public boolean findAdmin(String id) {
        Boolean admin=adminMapper.findAdmin(id);
        if(admin==null){
            return false;
        }
        return true;
    }


//    public void updatePassword(String id) {
//        AdminPageDTO  adminPageDTO=adminMapper.getById(id);
//        Md5PasswordEncoder md5PasswordEncoder = new Md5PasswordEncoder();
//        RegisterDTO password = adminPageDTO.setPassword(md5PasswordEncoder.encode("123456"));
//        adminMapper.updatePassword(password);
//    }


}
